// import { queryUserInfo } from '~/api/user'
import { ElNotification } from 'element-plus'
import { getUserInfo as _getUserInfo } from '~/utils/auth'

export const useInitiate = () => {
    const token = useCookie('token')
    const isLogin = useState('isLogin')
    const userInfo = useState('userInfo')

    watchPostEffect(() => {
        if (import.meta.env.SSR) {
            return
        }
        isLogin.value = !!userInfo.value?.nickname
    })
    const getUserInfo = async () => {
        if (!token.value) {
            return
        }
        try {
            const data = _getUserInfo()
            // const { data } = await useGain(queryUserInfo())
            // console.log('getUserInfo userInfo:', data)
            userInfo.value = data
        } catch (error) {
            // console.log('getUserInfo error:', error)
            ElNotification.error('获取用户信息失败。')
        }
    }

    const updateUserInfo = (value) => userInfo.value = value
    const removeUserInfo = () => {
        userInfo.value = null
    }

    const initData = async () => {
        await Promise.all([
            getUserInfo
        ])
        await getUserInfo()
    }

    return {
        isLogin,
        userInfo,
        getUserInfo,
        updateUserInfo,
        removeUserInfo,
        initData
    }
}